#import "RNGradientBox.h"

static NSString* const RNGradientBoxBackgroundGradientKey = @"RNGradientBoxBackgroundGradientKey";
static NSString* const RNGradientBoxBorderColorKey = @"RNGradientBoxBorderColorKey";

@implementation RNGradientBox

@synthesize backgroundGradient = backgroundGradient_;
@synthesize borderColor = borderColor_;

-(id)initWithFrame:(NSRect)frameRect {
    if (!(self = [super initWithFrame:frameRect]))
        return nil;
    [self setBackgroundGradient:[[NSGradient alloc] initWithStartingColor:[NSColor blackColor] endingColor:[NSColor whiteColor]]];
    [self setBorderColor:[NSColor blackColor]];
    [self setTitlePosition:NSNoTitle];
    [self setContentViewMargins:NSZeroSize];
    [self setBorderType:NSNoBorder];
    return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder {
    [super encodeWithCoder:aCoder];
    [aCoder encodeObject:[self backgroundGradient] forKey:RNGradientBoxBackgroundGradientKey];
    [aCoder encodeObject:[self borderColor] forKey:RNGradientBoxBorderColorKey];
}

-(id)initWithCoder:(NSCoder *)aDecoder {
    if (!(self = [super initWithCoder:aDecoder]))
        return nil;
    [self setBackgroundGradient:[aDecoder decodeObjectForKey:RNGradientBoxBackgroundGradientKey]];
    [self setBorderColor:[aDecoder decodeObjectForKey:RNGradientBoxBorderColorKey]];
    return self;
}

-(void)drawRect:(NSRect)dirtyRect {
    NSRect bounds = [self bounds];
    [[self backgroundGradient] drawInRect:bounds angle:90];
    [[self borderColor] setStroke];
    [NSBezierPath strokeLineFromPoint:bounds.origin toPoint:NSMakePoint(NSMaxX(bounds), NSMinY(bounds))];
}

@end
